import { getUserChannels, getChannels } from '@/api/channels'
export default {
  namespaced: true,
  state: {
    // 我的频道数据
    channelList: [],
    allChannelList: []
  },
  mutations: {
    setChannelList(state, payload) {
      state.channelList = payload
    },
    addChannelList(state, payload) {
      state.channelList.push(payload)
    },
    delChannelList(state, payload) {
      state.channelList = state.channelList.filter(
        (item) => item.id !== payload.id
      )
    },
    setAllChannelList(state, payload) {
      state.allChannelList = payload
    }
  },
  actions: {
    async actions_getChannelList(context) {
      const res = await getUserChannels()
      console.log(res)
      // this.channelList = res.data.channels
      context.commit('setChannelList', res.data.channels)
    },
    async actions_getAllChannelList(context) {
      const res = await getChannels()
      console.log(res)
      context.commit('setAllChannelList', res.data.channels)
    }
  },
  getters: {
    // 所有频道数据-我的频道数据 ==> 可选频道数据

    optionChannelList(state) {
      // 从所有频道遍历 return 过滤出我的频道 !false没有的数据
      return state.allChannelList.filter((item) => {
        // ⭐⭐外部循环一遍内部执行所有，将channelList每一项与item比较
        return !state.channelList.some((channel) => {
          // ⭐true 表示我的频道里有
          // ⭐false 表示我的频道里没有 取反 item返回出去组成新数组
          return channel.id === item.id
        })
      })
    }
  }
}
